package com.webserver.controllers;

import com.webserver.http.HttpRequest;
import com.webserver.http.HttpResponse;
import com.webserver.vo.Article;

import java.io.*;
import java.util.ArrayList;
import java.util.List;

public class ArticleController {
    private static String articleDri ="./articles/";
    static {
        File articleDir =new File(articleDri);
        if (!articleDir.exists()){
            articleDir.mkdirs();
        }
    }
    public void writeArticle(HttpRequest request, HttpResponse response){
        System.out.println("处理发表文章操作！！！");
        String title =request.getParameter("title");
        String author =request.getParameter("author");
        String content =request.getParameter("content");





        if (title==null || author==null || content==null){
            File fail =new File("./webapps/myweb/article_fail.html");
            response.setEntity(fail);
            return;
        }
        File articleFile =new File(articleDri+title+".obj");
        if (articleFile.exists()){
            File fail =new File("./webapps/myweb/article_fail.html");
            response.setEntity(fail);
            return;
        }
        try (ObjectOutputStream oos =new ObjectOutputStream(
                new FileOutputStream(articleFile)
        )
        ){
            Article article =new Article(title,author,content);
            oos.writeObject(article);
            File success =new File("./webapps/myweb/article_success.html");
            response.setEntity(success);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    public void showAllArticle(HttpRequest request,HttpResponse response){
        System.out.println("开始处理文章列表！！");
        List<Article> articleList =new ArrayList<>();
        File dir =new File(articleDri);
        File[] subs =dir.listFiles(f->f.getName().endsWith(".obj"));
        for (File articleFile :subs){
            try(ObjectInputStream ois =new ObjectInputStream(
                    new FileInputStream(articleFile)
            )
            ) {
                Article article =(Article) ois.readObject();
                articleList.add(article);
            } catch (IOException | ClassNotFoundException e) {
                e.printStackTrace();
            }
        }
        PrintWriter pw =response.getWriter();
        pw.println("<!DOCTYPE html>");
        pw.println("<html lang=\"en\">");
        pw.println("<head>");
        pw.println("<meta charset=\"UTF-8\">");
        pw.println("<title>文章列表</title>");
        pw.println("</head>");
        pw.println("<body>");
        pw.println("<center>");
        pw.println("<h1>文章列表</h1>");
        pw.println("<table border=\"1\">");
        pw.println("<tr>");
        pw.println("<td>标题</td>");
        pw.println("<td>作者</td>");
        pw.println("</tr>");
        for (Article article : articleList) {
            pw.println("<tr>");
            pw.println("<td>" + article.getTitle() + "</td>");
            pw.println("<td>" + article.getAuthor() + "</td>");
            pw.println("</tr>");
        }
        pw.println("</table>");
        pw.println("</center>");
        pw.println("</body>");
        pw.println("</html>");
        response.setContentType("text/html");
    }
}
